package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.Role;
import com.yqg.protection.entity.RoleMenu;
import com.yqg.protection.entity.User;
import com.yqg.protection.service.IRoleMenuService;
import com.yqg.protection.service.IRoleService;
import com.yqg.protection.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * RoleController 前端控制器
 *
 * @author zxf
 * @since 2024-01-03
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;
    @Autowired
    private IRoleMenuService roleMenuService;
    @Resource
    private IUserService userService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改角色")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody Role role) {
        if (role.getId() == null) {
            role.setIsDelete("0");
            roleService.saveOrUpdate(role);
            ArrayList<RoleMenu> roleMenus = new ArrayList<>();
            List<Long> menuIds = role.getMenuIds();
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(role.getId());
                roleMenus.add(roleMenu);
            }
            return Result.success(roleMenuService.saveBatch(roleMenus));
        } else {
            role.getMenuIds().add(role.getId());
            roleService.saveOrUpdate(role);
            return Result.success(roleMenuService.updateRoleMenu(role.getMenuIds()));
        }
    }

    /**
     * 删除时先进行判断
     */
    @GetMapping("delete/{id}")
    @SysLog("删除角色")
    public Result delete(@PathVariable Integer id) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getRole, id);
        List<User> list = userService.list(userLambdaQueryWrapper);
        if (list.size() > 0) {
            return Result.error("500", "当前角色下存在用户，禁止删除");
        }
        Role role = roleService.getById(id);
        role.setIsDelete("1");
        return Result.success(roleService.saveOrUpdate(role));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Role> list = roleService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(roleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

